package cn.szzsi._web;

import cn.szzsi.common.bean.dto.Msg;
import cn.szzsi.common.model.IntegralOrders;
import cn.szzsi.common.model.IntegralProduct;
import cn.szzsi.core.kit.UtilDate;
import com.jfinal.aop.Clear;
import com.jfinal.plugin.activerecord.Page;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 前台类积分兑换处理
 * Created by zhengy on 2017/2/7.
 */
public class IntegralProductController extends BaseWebController<IntegralProduct>{
    private IntegralProduct integralProduct;
    //积分兑换接口(所有商品)
    @Clear
    public void getIntegralProduct(){
        //String totalIntegral=getPara("totalIntegral");
        int pageNumber=getParaToInt("pageNumber", 1);//第几页,默认1
        int pageSize=getParaToInt("pageSize", 10);//每页的数量,默认10
        //Map<String,Object> map=new HashMap<>();
        StringBuilder sql=new StringBuilder("from mall_integral_product where isMarketable=1 and type='0' and shelfTime>(select now() as Systemtime)");
        Page<IntegralProduct> pager = IntegralProduct.dao.paginate(pageNumber,pageSize,"select id,name,img,needIntegral,needXin,store ",sql.toString());
        //System.out.println(JsonKit.toJson(pager));
        //List<IntegralProduct> list=IntegralProduct.dao.find("select id,name,img,needIntegral, from mall_integral_product where isMarketable=1 and shelfTime>(select now() as Systemtime)");
        //map.put("pager",pager);
        renderJson(Msg.success(pager));
    }

    //积分兑换接口(商品详情)
    @Clear
    public void getProductDetail(){
        Map<String,Object> map=new HashMap<String,Object>();
        String id=getPara("id");
        integralProduct= IntegralProduct.dao.findFirst("select id,name,img,needIntegral,needXin,description,store from mall_integral_product where id=?",id);
        map.put("product",integralProduct);
        renderJson(Msg.success(integralProduct));
    }

    //积分兑换接口(确认订单页)
    @Clear
    public void rightExchange(){
        String id=getPara("id");//商品id
        Integer totalIntegral=getParaToInt("totalXin");//医生总积分
        String customerId=getPara("customerId");//医生id
        integralProduct= IntegralProduct.dao.findById(id);
        if(totalIntegral<integralProduct.getInt("needXin")){
            renderJson(Msg.fail("心不足!",""));
            return ;
        }else{
            Map<String,Object> map=new HashMap<String,Object>();
            integralProduct= IntegralProduct.dao.findFirst("select id,name,needIntegral,needXin from mall_integral_product where id=?",id);
            IntegralOrders integralOrders= IntegralOrders.dao.findFirst("select * from mall_integral_orders where customerId=? order by createDate desc limit 1",customerId);
            if(integralOrders!=null){//上次购物用户信息
                map.put("name",integralOrders.getStr("name"));
                map.put("phone",integralOrders.getStr("phone"));
                map.put("province",integralOrders.getStr("province"));
                map.put("city",integralOrders.getStr("city"));
                map.put("area",integralOrders.getStr("area"));
                map.put("address",integralOrders.getStr("address"));
            }else{
                map.put("name","");
                map.put("phone","");
                map.put("province","");
                map.put("city","");
                map.put("area","");
                map.put("address","");
            }
            map.put("integralProduct",integralProduct);
            renderJson(Msg.success(map));
            //renderJson(Msg.success(integralProduct));
        }
    }

    //积分兑换接口(确认兑换)
    @Clear
    public void sureExchange(){
        String customerId=getPara("customerId");//用户id
        String productId=getPara("productId");//商品id
        String name=getPara("name");//收货人名称
        String phone=getPara("phone");
        String province=getPara("province");
        String city=getPara("city");
        String area=getPara("area");
        String address=getPara("address");
        String testphone = "0?(13[0-9]|15[0-9]|17[0-9]|18[0-9]|14[57])[0-9]{8}";
        integralProduct= IntegralProduct.dao.findById(productId);
        Integer totalIntegral=getParaToInt("totalXin");//医生总积分
        if(!phone.matches(testphone)){
            renderJson(Msg.fail("手机号码格式错误!",""));
            return;
        }
        if(totalIntegral<integralProduct.getInt("needXin")){
            renderJson(Msg.fail("愛心不足!",""));
            return ;
        }
        if(integralProduct.getInt("store")<1){
            renderJson(Msg.fail("库存不足!",""));
            return ;
        }
        List<IntegralOrders> list= IntegralOrders.dao.getByCustomerIdAndProductId(customerId,productId);
        if(list.size()>=integralProduct.getInt("exchangeLimit")){
            renderJson(Msg.fail("兑换次数已达上限",""));
            return;
        }
        String orderId= IntegralOrders.dao.saveOrders(customerId,productId,name,phone,province,city,area,address,integralProduct.getInt("needXin"));
        integralProduct.set("store",integralProduct.getInt("store")-1).update();//兑换成功,库存减1
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("needXin",integralProduct.getInt("needXin"));
        map.put("orderId",orderId);
        renderJson(Msg.success(map));

    }
    //积分兑换接口(兑换记录)
    @Clear
    public void exchangeRecords(){
        String customerId=getPara("customerId");//医生id
        int pageNumber=getParaToInt("pageNumber", 1);//第几页,默认1
        int pageSize=getParaToInt("pageSize", 10);//每页的数量,默认10
        List<Object> list=new ArrayList<Object>();
        StringBuilder sql=new StringBuilder("from mall_integral_orders where customerId='"+customerId+"' order by createDate desc");
        Page<IntegralOrders> pager = IntegralOrders.dao.paginate(pageNumber,pageSize,"select * ",sql.toString());
        Map<String,Object> pageMap=new HashMap<String,Object>();
        pageMap.put("pageNumber",pageNumber);
        pageMap.put("pageSize",pageSize);
        pageMap.put("totalPage",pager.getTotalPage());
        pageMap.put("totalRow",pager.getTotalRow());
        pageMap.put("firstPage",pager.isFirstPage());
        pageMap.put("lastPage",pager.isLastPage());

        for(IntegralOrders integralOrders:pager.getList()){
            Map<String,Object> map=new HashMap<String,Object>();
            try {
                map.put("createTime", UtilDate.strToTimestamp(integralOrders.getDate("createDate").toString()));//兑换时间
            } catch (ParseException e) {
                e.printStackTrace();
            }
            String productId=integralOrders.getStr("productId");
            integralProduct= IntegralProduct.dao.findById(productId);
            if(integralProduct!=null){
                map.put("productId",integralProduct.getStr("id"));
                map.put("name",integralProduct.getStr("name"));
                map.put("img",integralProduct.getStr("img"));
            }else{
                map.put("productId","");
                map.put("name","");
                map.put("img","");
            }

            list.add(map);
        }
        pageMap.put("list",list);
        renderJson(Msg.success(pageMap));

    }


}
